﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using web.data.entities;
using System.Configuration;
using Webdiyer.WebControls.Mvc;
using System.Web.UI;
using System.Linq.Expressions;
using System.Reflection;
using System.Xml.Schema;

namespace web.bussines
{
    public class language_service : base_service
    {
        //get list language, status =-1: get all
        public PagedList<language> adm_lang_cate_list(int pageindex, int pagesize, int language_status)
        {
            using (var data = new baseEntities())
            {
                var list = (from lg in data.tbl_language
                            where lg.language_status == language_status || language_status == -1
                            orderby lg.language_id ascending
                            select lg);
                list = list.OrderByDescending(p => p.language_id);
                return list.ToPagedList(pageindex, pagesize);
            }
        }

        public int adm_lang_update(language item)
        {
            using (var data = new baseEntities())
            {
                var edit_item = data.tbl_language.Where(p => p.language_id == item.language_id).SingleOrDefault();
                if (edit_item != null)
                {
                    edit_item.language_code = item.language_code;
                    edit_item.language_desc = item.language_desc;
                    edit_item.language_flag = item.language_flag;
                    edit_item.language_status = item.language_status;
                    data.SaveChanges();
                    return edit_item.language_id;
                }
                else
                {
                    data.AddTotbl_language(item);
                    data.SaveChanges();
                    return item.language_id;
                }
            }
        }
        //enable - disable language
        public int adm_lang_enable(int language_id, byte language_status)
        {
            var rt = 0;
            using (var data = new baseEntities())
            {
                var edit_item = data.tbl_language.Where(p => p.language_id == language_id).SingleOrDefault();
                //var language_status = 0;
                //if (is_enable == true)
                //    language_status = 1;
                if (edit_item != null) {
                    edit_item.language_status = Convert.ToByte(language_status);
                    rt = adm_lang_update(edit_item);
                }
            } return rt;
        }

        //get items by id or code
        public language getByID(int id, string lang_code)
        {
            using (var data = new baseEntities())
            {
                var item = data.tbl_language.Where(p => p.language_id == id || p.language_code == lang_code.Trim()).FirstOrDefault();
                return item;
            }
        }

        //get list language Enable
        public List<language> get_list_language()
        {
            using (var data = new baseEntities())
            {
                var list = data.tbl_language.Where(p => p.language_status == 1).ToList();
                return list;
            }
        }

    }
}
